package com.boot.mgt.service.impl


import ch.qos.logback.core.net.SyslogOutputStream
import com.boot.core.result.ResultTo
import com.boot.mgt.dao.mysql.RoadRepairMapper
import com.boot.mgt.dao.mysql.RoadRepairImageMapper

import com.boot.mgt.model.TaRoadRepair
import com.boot.mgt.model.TaRoadRepairImage
import com.boot.mgt.service.RoadRepairService
import com.github.pagehelper.PageHelper
import com.github.pagehelper.PageInfo
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.text.SimpleDateFormat
import java.util.*


/**
 *
 * xiaying
 */
@AutoJsonRpcServiceImpl
@Service
class RoadRepairServiceImpl : RoadRepairService {



    @Autowired
    protected lateinit var roadRepairMapper: RoadRepairMapper
    @Autowired
    protected lateinit var roadRepairImageMapper: RoadRepairImageMapper


    override fun list(roadName:String?,createTime_begin:String?, createTime_end:String?): ResultTo {

        val list = with(TaRoadRepair()) {
            val example = Example(TaRoadRepair::class.java)
            val  criteria = example.createCriteria()
            if(roadName!=null&&!roadName.trim().equals("")){

            criteria.andLike("road", "%"+roadName+"%")
            }
            criteria.andBetween("createTime",createTime_begin,createTime_end)
            example.and(criteria)
            example.orderBy("createTime").desc()
            roadRepairMapper.selectByExample(example)
        }
        return ResultTo().apply {
            this["road_list"] = list
        }
    }




    override fun addRoad(taRoadRepair: TaRoadRepair): ResultTo {
        roadRepairMapper.insert(taRoadRepair)
        return ResultTo()
    }
    override fun editRoadXia(taRoadRepair: TaRoadRepair): ResultTo {
        var oldPoint:TaRoadRepair = roadRepairMapper.selectByPrimaryKey(taRoadRepair.id)
            taRoadRepair.police=oldPoint.police
            taRoadRepair.createTime=oldPoint.createTime
        roadRepairMapper.updateByPrimaryKeySelective(taRoadRepair)
        return ResultTo()
    }
    override fun editRoad(taRoadRepair: TaRoadRepair): ResultTo {
        var oldPoint:TaRoadRepair = roadRepairMapper.selectByPrimaryKey(taRoadRepair.id)
        taRoadRepair.createTime = oldPoint.createTime;
        taRoadRepair.police = oldPoint.police;
        taRoadRepair.longitude = oldPoint.longitude;
        taRoadRepair.latitude = oldPoint.latitude;
        if(taRoadRepair.road == null || "".equals(taRoadRepair.road)){
            taRoadRepair.road = oldPoint.road;
        }
        if(taRoadRepair.measures == null || "".equals(taRoadRepair.measures)){
            taRoadRepair.measures = oldPoint.measures;
        }
        if(taRoadRepair.problem == null || "".equals(taRoadRepair.problem)){
            taRoadRepair.problem = oldPoint.problem;
        }
        roadRepairMapper.updateByPrimaryKey(taRoadRepair)
        return ResultTo()
    }

    override fun detail(roadId: Int): ResultTo {
        var taRoadRepairTemp = TaRoadRepair()
        taRoadRepairTemp.id = roadId
        var taRoadRepair= roadRepairMapper.selectOne(taRoadRepairTemp)
        return ResultTo().apply {
            this["taRoadRepair"] =taRoadRepair

        }


    }
    override fun delete(roadId: Int): ResultTo {
      roadRepairMapper.deleteByPrimaryKey(roadId)

        return ResultTo()
    }

    override  fun getImgList(page_index: Int, page_size: Int, roadId:Int):ResultTo{

        val list = with(TaRoadRepairImage()) {
            PageHelper.startPage<Any>(page_index, 5)
            val example = Example(TaRoadRepairImage::class.java)
            val  criteriaTemp = example.createCriteria()
            criteriaTemp.andEqualTo("roadId", roadId)
            example.and(criteriaTemp)
            example.orderBy("creatTime").desc()
            roadRepairImageMapper.selectByExample(example)
        }
            return ResultTo().apply {
            val pageInfo = PageInfo(list)
            this["repair_image_list"] = list
            this["page_index"] = page_index
            this["page_amount"] = pageInfo.pages
            this["page_total"] = pageInfo.total
        }
    }

    override fun uploadImg(policeNum:String,roadId:Int,imgList: Array<String>): ResultTo {

        for(element in imgList){
            var taRoadRepairImage=TaRoadRepairImage()
            taRoadRepairImage.creatPoliceNumber=policeNum
            taRoadRepairImage.creatTime=SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Date())
            taRoadRepairImage.roadId=roadId
            taRoadRepairImage.imageconent=element.toString()
            roadRepairImageMapper.insert(taRoadRepairImage)
        }

        return ResultTo()
    }
    override fun uploadOneImg(policeNum:String,roadId:Int,img: String): ResultTo {

            var taRoadRepairImage=TaRoadRepairImage()
            taRoadRepairImage.creatPoliceNumber=policeNum
            taRoadRepairImage.creatTime=SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Date())
            taRoadRepairImage.roadId=roadId
            taRoadRepairImage.imageconent=img
            roadRepairImageMapper.insert(taRoadRepairImage)

        return ResultTo()
    }

     override  fun delImg(id:Int):ResultTo{
         roadRepairImageMapper.deleteByPrimaryKey(id)

         return ResultTo()
     }
}

